/************************************
*  Copyright (C) 2010 Julien MOTTIN.
*  Contact: julien.mottin@gueydan.eu
*
*  This file is part of Gueydan.
*
*  Gueydan is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  Gueydan is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with Gueydan.  If not, see <http://www.gnu.org/licenses/>.
*
*************************************/
// local
#include "duration.h"
#include "util.h"

// QtCore
#include <QMap>
#include <QString>

Duration::Duration(const double iDuration, const DurationUnit iUnit)
{
    _Value = iDuration;
    switch (iUnit)
    {
    case Minute:
        break;
    case Hour:
        _Value*=DCoeffHour;
        break;
    case Day:
        _Value*=DCoeffDay;
        break;
    case Week:
        _Value*=DCoeffWeek;
        break;
    case Second:
        _Value*=DCoeffSecond;
        break;
    }
}

Duration::Duration(const Duration &iD)
{
    _Value = iD._Value;
}

Duration & Duration::operator=(const Duration &iD)
{
    if (this!=&iD)
    {
        _Value = iD._Value;
    }
    return *this;
}

Duration::~Duration()
{
}

bool Duration::operator==(const Duration &iD) const
{
    return (_Value==iD._Value);
}

bool Duration::operator!=(const Duration &iD) const
{
    return (_Value!=iD._Value);
}

const Duration Duration::operator+(const Duration &iD) const
{
    return Duration(_Value+iD._Value,Minute);
}

const Duration Duration::operator-(const Duration &iD) const
{
    return Duration(_Value-iD._Value,Minute);
}

bool Duration::operator<(const Duration &iD) const
{
    return (Compare(iD)<0);
}

bool Duration::operator<=(const Duration &iD) const
{
    return (Compare(iD)<=0);
}

bool Duration::operator>(const Duration &iD) const
{
    return (Compare(iD)>0);
}

bool Duration::operator>=(const Duration &iD) const
{
    return (Compare(iD)>=0);
}

double Duration::Value(DurationUnit iUnit) const
{
    double oVal = _Value;
    switch (iUnit)
    {
    case Minute:
        break;
    case Hour:
        oVal/=DCoeffHour;
        break;
    case Day:
        oVal/=DCoeffDay;
        break;
    case Week:
        oVal/=DCoeffWeek;
        break;
    case Second:
        oVal/=DCoeffSecond;
        break;
    }
    return oVal;
}

// Global Duration QMap
static QMap<QString,DurationUnit> DurationUnitMap;
static bool DurationUnitMapInit = false;

void Duration::FromString(const QString &iString, bool *OK)
{
    bool tmpOK = false;

    if (!DurationUnitMapInit)
    {
        DurationUnitMap["m"] = Minute;
        DurationUnitMap["min"] = Minute;
        DurationUnitMap["h"] = Hour;
        DurationUnitMap["hour"] = Hour;
        DurationUnitMap["hours"] = Hour;
        DurationUnitMap["hr"] = Hour;
        DurationUnitMap["d"] = Day;
        DurationUnitMap["day"] = Day;
        DurationUnitMap["days"] = Day;
        DurationUnitMap["w"] = Week;
        DurationUnitMap["wk"] = Week;
        DurationUnitMap["week"] = Week;
        DurationUnitMap["s"] = Second;
        DurationUnitMap["sec"] = Second;
        DurationUnitMapInit = true;
    }

    QString Unit;
    double Val;
    StringToUnit(iString,Val,Unit);

    // Case no Unit
    if (Unit.isEmpty())
    {
        // Just copy value
        _Value = Val;
        tmpOK = true;
    }
    else
    {
        if (DurationUnitMap.contains(Unit))
        {
            // Build duration with proper unit
            DurationUnit zeUnit = DurationUnitMap[Unit];
            (*this)=Duration(Val,zeUnit);
            tmpOK = true;
        }
    }

    if (0!=OK)
        (*OK)=tmpOK;
}

double Duration::Compare(const Duration &iD) const
{
    return (_Value - iD._Value);
}
